package com.snail.poker.web.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.json.annotations.JSON;

import com.snail.poker.model.Equipment;
import com.snail.poker.model.Innings;
import com.snail.poker.model.Opponents;
import com.snail.poker.model.Rank;
import com.snail.poker.service.InningService;
import com.snail.poker.uitls.Constant;
import com.snail.poker.web.bean.PageBean;

/**
 * 牌局记录的Action
 * 
 * @author Snail
 * 
 */
public class InningsAction extends BaseAction {
	private static final Log log = LogFactory.getLog(InningsAction.class);
	private InningService inningsManage;
	private Innings innings;
	private int inningSn;
	private int insureAmong;
	private List<Rank> ranks;
	private List<Opponents> opponents;
	private PageBean pageBean;
	private InputStream excelStream;

	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	@JSON(serialize = false)
	public int getInsureAmong() {
		return insureAmong;
	}

	public void setInsureAmong(int insureAmong) {
		this.insureAmong = insureAmong;
	}

	@JSON(serialize = false)
	public int getInningSn() {
		return inningSn;
	}

	public void setInningSn(int inningSn) {
		this.inningSn = inningSn;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	@JSON(serialize = false)
	public List<Opponents> getOpponents() {
		return opponents;
	}

	public void setOpponents(List<Opponents> opponents) {
		this.opponents = opponents;
	}

	@JSON(serialize = false)
	public List<Rank> getRanks() {
		return ranks;
	}

	public void setRanks(List<Rank> ranks) {
		this.ranks = ranks;
	}

	public Innings getInnings() {
		return innings;
	}

	public void setInnings(Innings innings) {
		this.innings = innings;
	}

	@JSON(serialize = false)
	public InningService getInningsManage() {
		return inningsManage;
	}

	public void setInningsManage(InningService inningsManage) {
		this.inningsManage = inningsManage;
	}

	@Override
	public String remove() {
		// TODO Auto-generated method stub
		if (0 >= this.inningSn) {
			super.putMessage("无可操作数据.");
			return ERROR;
		}
		try {
			this.inningsManage.delInnings(inningSn);
			super.message = "删除成功.";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e);
			super.putMessage(e.getMessage());
			return ERROR;
		}
		return super.remove();
	}

	@Override
	public String input() {
		// TODO Auto-generated method stub
		if (null == this.innings) {
			super.putMessage("未发现可操作的数据.");
			return ERROR;
		}
		try {
			Equipment equip = (Equipment) super.session
					.get(Constant.EQUIPMENT_SESSION_KEY);
			this.innings.setEquipment(equip);
			this.innings.setCreateIp(super.getIpAddress());
			this.inningsManage.addInnings(this.innings, this.ranks,
					this.opponents);
			super.message = "记录成功.";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e);
			super.putMessage(e.getMessage());
			return ERROR;
		}
		return super.input();
	}

	@Override
	public String query() {
		try {
			this.pageBean = this.inningsManage.findInnings(innings,
					this.insureAmong);
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e);
			super.putMessage(e.getMessage());
			return ERROR;
		}
		return super.query();
	}

	@Override
	public String detail() {
		// TODO Auto-generated method stub
		if (0 >= this.inningSn) {
			super.putMessage("无可操作数据.");
			return ERROR;
		}
		try {
			this.innings = this.inningsManage.getInnings(inningSn);
			if (null == this.innings) {
				super.putMessage("没有该标识[ " + this.inningSn + " ]的数据.");
				return ERROR;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e);
			super.putMessage(e.getMessage());
			return ERROR;
		}
		return super.detail();
	}

	public String update() {
		if (null == this.innings) {
			super.putMessage("无可操作数据.");
			return ERROR;
		}
		try {
			this.inningsManage.modifyInnings(innings);
			super.message = "修改成功.";
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e);
			super.putMessage(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 文件导出
	 * 
	 * @return
	 */
	public String exprot() {
		try {
			this.excelStream = this.inningsManage.searchInnings(innings,
					this.insureAmong);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
